<template>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="书籍名称" prop="name">
      <el-input v-model="ruleForm.name"></el-input>
    </el-form-item>
    <el-form-item label="书籍作者" prop="author">
      <el-input v-model="ruleForm.author"></el-input>
    </el-form-item>
    <el-form-item label="书籍出版社" prop="publish">
      <el-input v-model="ruleForm.publish"></el-input>
    </el-form-item>
    <el-form-item label="书籍页数" prop="pages">
      <el-input v-model.number="ruleForm.pages"></el-input>
    </el-form-item>
    <el-form-item label="书籍价格" prop="price">
      <el-input v-model="ruleForm.price"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  data() {
    let checkPage = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('书籍页码不能为空'));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          if (value <= 0) {
            callback(new Error('页码必须大于0'));
          } else {
            callback();
          }
        }
      }, 500);
    };
    let isPrice = (value) => {
      //验证是否为准确的两位小数的金额
      // let pattern = /^(-)?\d+(\.\d+)?$/;
      let pattern = /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/;
      return !(pattern.exec(value) == null || value === "");
    };
    let checkPrice = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('书籍价格不能为空'));
      }
      setTimeout(() => {
        if (!isPrice(value)) {
          callback(new Error('价格有误，请重新输入'));
        } else {
          callback();
        }
      }, 500);
    };
    return {
      ruleForm: {
        name: '',
        author: '',
        publish: '',
        pages: '',
        price: ''
      },
      rules: {
        name: [
          {required: true, message: '请输入书籍名称', trigger: 'blur'},
          {min: 1, message: '长度至少为 1 个字符', trigger: 'blur'}
        ],
        author: [
          {required: true, message: '请输入作者姓名', trigger: 'change'}
        ],
        publish: [
          {required: true, message: '请输入出版社', trigger: 'change'}
        ],
        pages: [
          {validator: checkPage, trigger: 'blur'}
        ],
        price: [
          {validator: checkPrice, trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      const _this = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post('http://localhost:8081/book/save', _this.ruleForm).then(function (resp) {
            /*alert(_this.ruleForm.price);*/
            if (resp.data.result === 'success') {
              _this.$alert('《' + _this.ruleForm.name + '》添加成功！', '提示', {
                confirmButtonText: '确定',
                callback: action => {
                  /*添加成功之后的反馈动作*/
                  this.$message({
                    type: 'info',
                    message: 'action: ${action}'
                  });
                },
              });
              _this.$refs[formName].resetFields();
            }
          });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }
}
</script>

<style scoped>

</style>
